package com.ddz.shortcut.utils

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.graphics.Path
import android.graphics.Rect
import android.view.accessibility.AccessibilityNodeInfo

object ExeHelper {
    fun click(service: AccessibilityService,view:AccessibilityNodeInfo){
        if (!view.performAction(AccessibilityNodeInfo.ACTION_CLICK)){
            val point = Path()
            val viewRect = Rect()
            view.getBoundsInScreen(viewRect)
            val x = viewRect.centerX()
            val y = viewRect.centerY()
            if (x<= 0 && y <= 0)return
            point.moveTo(x.toFloat(), y.toFloat())
            service.dispatchGesture(
                GestureDescription.Builder()
                    .addStroke(GestureDescription.StrokeDescription(point, 0, 50)).build(), object : AccessibilityService.GestureResultCallback() {
                    override fun onCompleted(gestureDescription: GestureDescription?) {
                        super.onCompleted(gestureDescription)
                    }

                    override fun onCancelled(gestureDescription: GestureDescription?) {
                        super.onCancelled(gestureDescription)
                    }
                }, null
            )
        }
    }
}